package com.itboye.inageview;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.luntan.comment.view.MyListView;

/**
 * 首页ListView的数据适配器
 * 
 * @author Administrator
 * 
 */
public class ListItemAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<GetAList> items;

	public ListItemAdapter(Context ctx, ArrayList<GetAList> items) {
		this.mContext = ctx;
		this.items = items;
	}

	@Override
	public int getCount() {
		return items == null ? 0 : items.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_list, null);
			holder.iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.tv_content = (TextView) convertView
					.findViewById(R.id.tv_content);
			holder.gridview = (NoScrollGridView) convertView
					.findViewById(R.id.gridview);
			holder.textGuanzhu = (TextView) convertView
					.findViewById(R.id.textGuanzhu);
			holder.tetzan = (TextView) convertView.findViewById(R.id.tetzan);
			holder.textTime = (TextView) convertView
					.findViewById(R.id.textTime);
			holder.lv_user_comment_replys = (MyListView) convertView
					.findViewById(R.id.lv_user_comment_replys);
			holder.tetPing = (TextView) convertView.findViewById(R.id.tetPing);
			holder.imgZan = (ImageView) convertView.findViewById(R.id.imgZan);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		GetAList itemEntity = items.get(position);
		holder.tv_title.setText(itemEntity.getAuthor());
		holder.tv_content.setText(itemEntity.getMessage());
		holder.textTime.setText(itemEntity.getDatelineShow());
		if (itemEntity.getHead() == null | "".equals(itemEntity.getHead())) {
			if ("1".equals(itemEntity.getSex())) {
				holder.iv_avatar.setImageResource(R.drawable.male);
			} else {
				holder.iv_avatar.setImageResource(R.drawable.female);
			}
		} else {
			if (itemEntity.getHead().startsWith("http")) {
				XImageLoader.load(itemEntity.getHead(), holder.iv_avatar);
			} else {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ itemEntity.getHead(), holder.iv_avatar);
			}
		}
		final ArrayList<String> imageUrls = itemEntity.getImg();
		if (imageUrls == null || imageUrls.size() == 0) { // 没有图片资源就隐藏GridView
			holder.gridview.setVisibility(View.GONE);
		} else {
			holder.gridview.setAdapter(new NoScrollGridAdapter(mContext,
					imageUrls));
		}
		if (itemEntity.getIsLike() != null) {
			if (itemEntity.getIsLike().equals("0")) {
				holder.imgZan.setBackgroundResource(R.drawable.like_select);
			} else {
				holder.imgZan.setBackgroundResource(R.drawable.like_unselect);
			}
		}
		// holder.lv_user_comment_replys.setAdapter(new
		// MySpaceCommentAdapter(mContext,itemEntity.getReplyInfo()));
		holder.tetzan.setText(itemEntity.getLikes());
		holder.tetPing.setText(itemEntity.getReplies());
		holder.textGuanzhu.setVisibility(View.GONE);
		// holder.tetzan.setVisibility(View.GONE);
		// 点击回帖九宫格，查看大图
		holder.gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				imageBrower(position, imageUrls);
			}
		});
		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}

	/**
	 * listview组件复用，防止“卡顿”
	 * 
	 * @author Administrator
	 * 
	 */
	class ViewHolder {
		private ImageView iv_avatar;
		private TextView tv_title, tetzan, tetPing;
		private TextView tv_content, textGuanzhu, textTime;
		private NoScrollGridView gridview;
		private MyListView lv_user_comment_replys;
		private ImageView imgZan;
	}
}
